package com.four.collage.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.four.collage.dao.GroupgoodsMapper;
import com.four.collage.dao.GroupmemberMapper;
import com.four.collage.entity.Groupgoods;
import com.four.collage.entity.Groupmanage;
import com.four.collage.dao.GroupmanageMapper;
import com.four.collage.entity.Groupmember;
import com.four.collage.feign.UserFeginservice;
import com.four.collage.service.IGroupmanageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.four.collage.vo.GoodsAndAttrVo;
import com.four.collage.vo.GroupManageVo;
import com.guli.common.utils.R;
import com.guli.common.vo.CityVo;
import com.guli.common.vo.UserVo;
import org.elasticsearch.gateway.Gateway;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-12-18
 */
@Service
public class GroupmanageServiceImpl extends ServiceImpl<GroupmanageMapper, Groupmanage> implements IGroupmanageService {

    @Autowired
    private GroupmanageMapper groupmanageMapper;

    @Autowired
    private GroupgoodsMapper groupgoodsMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private GroupmemberMapper groupmemberMapper;

    @Autowired
    private UserFeginservice userFeginservice;


    /**
     * 用户点击开团
     * 添加到拼团管理
     * @param grId
     * @param loginName
     */
    @Override
    public void addGroupmanage(Integer grId, String loginName) {
        //查询登录人信息
        UserVo userVo=userFeginservice.findByUname(loginName);
        //根据拼团商品id查询信息
        Groupgoods groupgoods=groupgoodsMapper.getGroupgoodsById(grId);
        //添加拼团管理
        Groupmanage groupmanage = new Groupmanage();
        groupmanage.setUserImg(userVo.getUserImg());
        groupmanage.setUserName(loginName);
        groupmanage.setGroStartTime(new Date());
        groupmanage.setGrName(groupgoods.getGrName());
        groupmanage.setGroPeopleNum(groupgoods.getGrSize());
        groupmanage.setGroParticipateNum(1);
        groupmanage.setGroEndTime(new Date());
        groupmanage.setGroStatus(0);
        groupmanageMapper.addGroupmanage(groupmanage);
        //添加到拼团记录表中
        Groupmember groupmember = new Groupmember();
        groupmember.setGmenName(loginName);
        groupmember.setGmenIocn(userVo.getUserImg());
        groupmember.setGmenStatus(0);
        groupmember.setGmenState(1);
        groupmember.setGmenStillbad(groupmanage.getGroPeopleNum()-1);
        groupmember.setGroId(grId);
        groupmemberMapper.addGropMember(groupmember);

        int id=groupgoodsMapper.slelectMaxId();
        rabbitTemplate.convertAndSend("order-die",id);
    }

    @Override
    public void updGroupmanageNum(Integer groId, String loginName) {
        //查询登录人信息
        UserVo userVo=userFeginservice.findByUname(loginName);
        //根据id查询数据
        Groupmanage groupmanage=groupmanageMapper.getGroupmanageById(groId);
        int participateNum=groupmanage.getGroParticipateNum()+1;
        //修改参加人数
        groupmanageMapper.updateParticipateNum(groId,participateNum);
        //添加到拼团记录
        Groupmember member = new Groupmember();
        member.setGmenName(loginName);
        member.setGmenIocn(userVo.getUserImg());
        member.setGmenStatus(1);
        member.setGmenState(1);
        member.setGroId(groId);
        member.setGmenStillbad(groupmanage.getGroPeopleNum()-participateNum);
        groupmemberMapper.addGropMember(member);
    }

    /**
     * 拼团管理列表、分页、模糊
     *
     * @param vo
     * @param page
     * @param size
     * @return
     */

    @Override
    public IPage<Groupmanage> groupManageList(GroupManageVo vo, Integer page, Integer size) {

        Page<Groupmanage> groupPage = new Page<>(page, size);
        return groupmanageMapper.groupManageList(groupPage,vo);
    }

}
